package com.lambkit.module.upms;

import com.lambkit.Lambkit;
import com.lambkit.plugin.rpc.RpcKit;
import com.lambkit.plugin.rpc.http.HttpRpcProxy;

public class UpmsService {

	public <T> T service(Class<T> serviceClass) {
		UpmsConfig config = UpmsManager.me().getConfig();
		System.out.println("http: " + config.getApiType() );
		if("http".equals(config.getApiType())) {
			System.out.println("http: " + config.getApiUrl() );
			return HttpRpcProxy.getObject(config.getApiUrl(), serviceClass);
		} else if("rpc".equals(config.getApiType())) {
			return (T) RpcKit.obtain(serviceClass);
		} else {
			return Lambkit.get(serviceClass);
		}
	}
	
	public <T> T service(Class<T> serviceClass, Class<T> serviceClassMock) {
		UpmsConfig config = UpmsManager.me().getConfig();
		if("http".equals(config.getApiType())) {
			T result = HttpRpcProxy.getObject(config.getApiUrl(), serviceClass);
			if(result==null) {
				return Lambkit.get(serviceClassMock);
			}
			return result;
		} else if("rpc".equals(config.getApiType())) {
			return (T) RpcKit.obtain(serviceClass, Lambkit.get(serviceClassMock));
		} else {
			return Lambkit.get(serviceClass, serviceClassMock);
		}
	}
}
